package com.lecture.springlecture.config;//package com.lecture.springlecture.config;
//
//import com.fasterxml.jackson.annotation.JsonAutoDetect;
//import com.fasterxml.jackson.annotation.JsonInclude;
//import com.fasterxml.jackson.annotation.PropertyAccessor;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.fasterxml.jackson.databind.SerializationFeature;
//import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
//import org.springframework.cache.CacheManager;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.data.redis.connection.RedisConnectionFactory;
//import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
//import org.springframework.data.redis.serializer.StringRedisSerializer;
//
///**
// * @program: spring-lecture
// * @description:
// * @author: luyn
// * @create: 2020-03-21 19:43
// **/
//@Configuration
//public class MyConfig {
//
//    private StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//    /** Jackson2JsonRedisSerializer 序列化和反序列化效率高 */
//    private Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//
//    @Bean
//    public MyConfigBean myConfigBean() {
//        System.out.println("初始化。。。。。。");
//        return new MyConfigBean();
//    }
//
//    /**
//     * 因 ZxlUser 没有 无参构造函数 反序列化时报错 故取消采用 jackson2JsonRedisSerializer 方式
//     * 改用默认JdkSerializationRedisSerializer.
//     */
//    @Bean
//    public CacheManager cacheManager(RedisConnectionFactory factory) {
//        // 解决查询缓存转换异常的问题
//        ObjectMapper om = new ObjectMapper();
//        //POJO无public的属性或方法时，不报错
//        om.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
//        // null值字段不显示
//        om.setSerializationInclusion(JsonInclude.Include.NON_NULL);
//        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        // 序列化JSON串时，在值上打印出对象类型
//        // om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//        // 替换上方 过期的enableDefaultTyping
//        om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance ,ObjectMapper.DefaultTyping.NON_FINAL,JsonTypeInfo.As.WRAPPER_ARRAY);
//        jackson2JsonRedisSerializer.setObjectMapper(om);
//
//        // 解决jackson2无法反序列化LocalDateTime的问题
//        om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
//        om.registerModule(new ZxlJavaTimeModule());
//
//        // 配置序列化（解决乱码的问题）
//        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
//                // 默认 30天过期
//                .entryTtl(Duration.ofDays(30))
//                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringRedisSerializer))
//                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
//                .disableCachingNullValues();
//
//        RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
//                .cacheDefaults(config)
//                .build();
//        return cacheManager;
//    }
//
//}
